/* written by Jonathan Odom and Aneesh Butani
 * 
 * Some of the string literals have been left in this code,
 * simply because this parser class will be completely redesigned
 * if not replaced within the next few days.  For the final
 * submission, the code will meet all of the required c++ conventions.
 */

#include "XMLParser.h"
#include "Appt.h"


string XMLParser::APPT = "appt";
string XMLParser::DAY = "day";
string XMLParser::TIME = "time";
string XMLParser::SUBJECT = "subject";
string XMLParser::start="START";
string XMLParser::end="END";
string XMLParser::description="DESCRIPTION";
string XMLParser::event="APPOINTMENT";
XMLParser::XMLParser()
{
}

XMLParser::~XMLParser()
{
}

XMLParser::XMLParser(const string& fileName)
: myFileName(fileName)
{
}

XMLParser::XMLParser(const string& fileName, int whichEvent)
: myFileName(fileName)
{
	if(whichEvent == 1)
	{
		readAppt();
	}
	else if(whichEvent == 2)
	{
		readBirthday();
	}
	else if (whichEvent == 3)
	{
		readAppt2();
	}
	else XMLParser(fileName);
	
}

void XMLParser::readAppt()
{
	ifstream f(myFileName.c_str());
	string node;
	string temp;
	while(!f.eof())
	{
		//getline does not include delimiter
		getline(f,temp, '<');
		getline(f,node, '>');
		if(node.at(1)!='!')
		{
			readNodeAppt(node);
		}
		
	}
	
}

void XMLParser::readNodeAppt(const string& node)
{
	string tempDay = getInfoAppt(node,DAY);
	string tempSubject = getInfoAppt(node,SUBJECT);
	int range[2]; 
	getTimeRangeAppt(node, TIME, range);
	Appt cur(atoi(tempDay.c_str()),range[0], range[1],tempSubject);
	myEvents.push_back(cur);
}

string XMLParser::getInfoAppt(const string& node, const string& infoType)
{
	unsigned int loc = node.find(infoType,0);
	if(loc != string::npos)
	{
		int start = node.find('\"', loc);
		int end = node.find('\"', start+1);
		return(node.substr(start+1,end-start-1));
	}
	return "Type not found";
}

void XMLParser::getTimeRangeAppt(const string& node, const string& infoType, int range[])
{
	range[0] = 0;
	range[1] = 0;

	unsigned int loc = node.find(infoType,0);
	if(loc != string::npos)
	{
		int start = node.find("\"",loc);
		int end = node.find("\"", start + 1);
		seperateTime(range, node.substr(start+1,end-start-1));
		
	}
}

void XMLParser::seperateTime(int range[], const string& times)
{
	unsigned int loc = times.find(".",0);
	unsigned int loc2 = times.find(",",0);
	if(loc != string::npos)
	{
		string temp = times.substr(0, loc);
		range[0]= atoi(temp.c_str());
		temp = times.substr(loc+1,times.length());
		range[1] = atoi(temp.c_str());
	}
	else if(loc2 != string::npos)
	{
		string temp = times.substr(0, loc2-1);
		range[0]= atoi(temp.c_str());
		temp = times.substr(loc2+1,times.length());
		range[1] = atoi(temp.c_str());
	}
}

vector<Event> XMLParser::getEvents()
{
	return myEvents;
}

void XMLParser::readBirthday()
{
	string date="DATE";
	string description="DESCRIPTION";
	string event="EVENT";
	ifstream f("Celebrity.xml");
	ofstream f2("Celebs.txt");
	while (!f.eof())
		getNode (f, f2, event, description, date);
}

void XMLParser::getNode(ifstream& f, ofstream& f2, string event, string description, string date)
{
string tag, data;
	string endevent="/"+event;
	tag=getTag(f);
	string tempDescription, tempDate;
		if (tag==event)
		{
			do
			{
				tag=getTag(f);
				if (tag!=endevent)
					{
						getline (f,data,'<'); 
							if (tag==description)
								tempDescription=data;
						if (tag==date)
							tempDate=data;
						f2<<tag<<endl<<data<<endl<<endl;
					}
			} while (tag!=endevent);
			Event cur(atoi(tempDate.c_str()), tempDescription);
			myEvents.push_back(cur);
		}
}

string XMLParser::getTag (ifstream& f)
{
	string tag;
	getline(f,tag,'<');
	getline (f,tag,'>');
	return tag;
}

void XMLParser::readAppt2()
{
	ofstream f2("crap.txt");
	ifstream f(myFileName.c_str());
	while (!f.eof())
		getNode (f, f2, event, description, start, end);
}


void XMLParser::getNode(ifstream& f, ofstream& f2, string event, string description, string start, string end )
{
	string tag, data,tempDescription, tempDate;
	double starttime, endtime;
	string endevent="/"+event;
	tag=getTag(f);
		if (tag==event)
		{
			do
			{
				tag=getTag(f);
				if (tag!=endevent)
					{
						getline (f,data,'<'); 
						if (tag==description)
							tempDescription=data;
						if (tag==start)
						{
							tempDate=data.substr(0,8);
							starttime=atoi(data.substr(9,2).c_str());
						}
						if (tag==end)
						{
							endtime=atoi(data.substr(9,2).c_str());
						}
					}
			} while (tag!=endevent);
			Event cur(atoi(tempDate.c_str()), starttime, endtime, tempDescription);
			myEvents.push_back(cur);
		}
}



